/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.lczyfy.idcp.modules.sys.vueweb;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.lczyfy.idcp.common.config.Global;
import com.lczyfy.idcp.common.utils.ResponseCode;
import com.lczyfy.idcp.common.utils.ServerResponse;
import com.lczyfy.idcp.common.utils.StringUtils;
import com.lczyfy.idcp.common.web.BaseController;
import com.lczyfy.idcp.modules.sys.entity.Area;
import com.lczyfy.idcp.modules.sys.service.AreaService;
import com.lczyfy.idcp.modules.sys.utils.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 区域Controller
 * @author ThinkGem
 * @version 2013-5-15
 */
@Api(value = "area")
@RestController
@RequestMapping(value = "${apiPath}/sys/area")
public class AreaVueController extends BaseController {

	@Autowired
	private AreaService areaService;
	@ApiOperation(value = "获取区域列表",notes = "")
	@RequiresPermissions("sys:area:view")
	@RequestMapping(value = "search",method = RequestMethod.GET) //菜单列表
	public ServerResponse<Area> search() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String createDate= df.format(new Date());
		Area area = new Area();
		List<Area> areas = areaService.findList(area);
		return new ServerResponse<Area>(ResponseCode.SUCCESS.getCode(),areas) ;
	}
	@ApiOperation(value = "添加和更新区域列表",notes = "根据区域创建区域")
	@RequiresPermissions("sys:area:edit")
	@RequestMapping(value = "save",method = RequestMethod.POST) //菜单列表
	public ServerResponse<String> create(@RequestBody Area area) {
		if(!UserUtils.getUser().isAdmin()){
			return ServerResponse.createByErrorMessage("越权操作，只有超级管理员才能添加或修改数据！");
		}
		Area parent =areaService.get(area.getAreaParentId());
		area.setParent(parent);
		areaService.save(area);
		return ServerResponse.createBySuccessMessage("保存区域成功");
	}
	@ApiOperation(value = "通过区域id详情",notes = "获取详情")
	@RequiresPermissions("sys:area:edit")
	@RequestMapping(value = "findById/{id}",method = RequestMethod.GET) //菜单列表
	public ServerResponse<Area> findById(@PathVariable String id) {
		if(Global.isDemoMode()){
			return  ServerResponse.createByErrorMessage("演示模式，不允许操作！");
		}
		Area findByIdArea = areaService.get(id);
		if(StringUtils.isNotBlank(findByIdArea.getParentId())){
		    findByIdArea.setAreaParentId(findByIdArea.getParentId());
        }
		return  new ServerResponse(ResponseCode.SUCCESS.getCode(),findByIdArea);
	}
	@ApiOperation(value = "删除区域列表",notes = "删除区域")
	@RequiresPermissions("sys:area:edit")
	@RequestMapping(value = "delete",method = RequestMethod.DELETE) //菜单列表
	public ServerResponse<String> delete(@RequestBody Area area) {
		if(Global.isDemoMode()){
			return ServerResponse.createByErrorMessage("演示模式，不允许操作！");
		}
		areaService.delete(area);
		return ServerResponse.deleteBySuccessMessage("删除区域成功");
	}
	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "treeData", method = RequestMethod.GET)
	@ApiOperation(value = "获取区域组织机构")
	public ServerResponse<List<Map<String, Object>>> treeData(@RequestParam(required=false) String extId, HttpServletResponse response) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
		String createDate= df.format(new Date());
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<Area> list = areaService.findAll();
		for (int i=0; i<list.size(); i++){
			Area e = list.get(i);
			if (StringUtils.isBlank(extId) || (extId!=null && !extId.equals(e.getId()) && e.getParentIds().indexOf(","+extId+",")==-1)){
				Map<String, Object> map = Maps.newHashMap();
				map.put("id", e.getId());
				map.put("pId", e.getParentId());
				map.put("name", e.getName());
				mapList.add(map);
			}
		}
		return  new ServerResponse(ResponseCode.SUCCESS.getCode(), mapList);
	}
}
